﻿using System;
using System.Text;

namespace MyVBAScript
{
    /// <summary>
    /// 在VB.NET脚本中使用的 window 对象类型
    /// </summary>
    public class XVBAWindowObject : System.IDisposable
    {
        public XVBAWindowObject(
            System.Windows.Forms.IWin32Window win,
            XVBAEngine engine,
            string systemName)
        {
            this.myParentWindow = win;
            this.myEngine = engine;
            strSystemName = systemName;
            this.bolUserInteractive = System.Windows.Forms.SystemInformation.UserInteractive;
        }

        public XVBAWindowObject()
        {
            this.bolUserInteractive = System.Windows.Forms.SystemInformation.UserInteractive;
        }

        protected bool bolUserInteractive = true;
        /// <summary>
        /// 是否允许和用户交互，也就是是否显示用户界面
        /// </summary>
        /// <remarks>当应用程序为ASP.NET或者Windows Service程序时不能有图形化用户界面，因此需要设置该属性为false.</remarks>
        public bool UserInteractive
        {
            get { return bolUserInteractive; }
            set { bolUserInteractive = value; }
        }

        protected string strSystemName = "应用程序";
        /// <summary>
        /// 系统名称
        /// </summary>
        public string SystemName
        {
            get
            {
                return strSystemName;
            }
            set
            {
                strSystemName = value;
            }
        }

        protected XVBAEngine myEngine = null;
        /// <summary>
        /// 脚本引擎对象
        /// </summary>
        public XVBAEngine Engine
        {
            get { return myEngine; }
        }

        protected System.Windows.Forms.IWin32Window myParentWindow = null;
        /// <summary>
        /// 父窗体对象
        /// </summary>
        public System.Windows.Forms.IWin32Window ParentWindow
        {
            get { return myParentWindow; }
            set { myParentWindow = value; }
        }

        /// <summary>
        /// 屏幕宽度
        /// </summary>
        public int ScreenWidth
        {
            get
            {
                if (bolUserInteractive)
                {
                    return System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width;
                }
                else
                {
                    return 0;
                }
            }
        }

        /// <summary>
        /// 屏幕高度
        /// </summary>
        public int ScreenHeight
        {
            get
            {
                if (bolUserInteractive)
                {
                    return System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height;
                }
                else
                {
                    return 0;
                }
            }
        }

        #region 延时调用的代码群 ******************************************************************

        private string strTimeoutMethod = null;
        private System.Windows.Forms.Timer myTimer;
        /// <summary>
        /// 设置延时调用
        /// </summary>
        /// <param name="MinSecend">延时的毫秒数</param>
        /// <param name="MethodName">调用的方法名称</param>
        public void SetTimeout(int MinSecend, string MethodName)
        {
            // 若不支持和用户互换则本功能无效
            if (bolUserInteractive == false)
                return;
            if (myEngine == null)
                return;
            if (myIntervalTimer != null)
            {
                // 取消当前的定时调用功能
                myIntervalTimer.Stop();
            }
            strTimerIntervalMethod = null;
            if (myTimer == null)
            {
                // 若定时器不存在则创建新的定时器对象
                myTimer = new System.Windows.Forms.Timer();
                myTimer.Tick += new EventHandler(myTimer_Tick);
            }
            // 设置定时器
            myTimer.Interval = MinSecend;
            // 设置脚本方法名称
            strTimeoutMethod = MethodName;
            // 启动定时器
            myTimer.Start();
        }
        /// <summary>
        /// 清除延时调用
        /// </summary>
        public void ClearTimeout()
        {
            if (myTimer != null)
            {
                // 停止定时器
                myTimer.Stop();
            }
            // 清空延时调用的脚本方法名称
            strTimeoutMethod = null;
        }

        /// <summary>
        /// 延时调用的定时器事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void myTimer_Tick(object sender, EventArgs e)
        {
            myTimer.Stop();
            if (myEngine != null && strTimeoutMethod != null)
            {
                // 获得脚本方法
                string m = strTimeoutMethod.Trim();
                strTimeoutMethod = null;
                if (myEngine.HasMethod(m))
                {
                    // 若脚本引擎中定义了该名称的脚本方法则安全的执行该脚本方法
                    myEngine.ExecuteSub(m);
                }
            }
        }

        #endregion

        #region 定时调用的代码群 ******************************************************************

        /// <summary>
        /// 定时调用使用的定时器控件
        /// </summary>
        private System.Windows.Forms.Timer myIntervalTimer = null;
        /// <summary>
        /// 定时调用的脚本方法的名称
        /// </summary>
        private string strTimerIntervalMethod = null;

        /// <summary>
        /// 设置定时运行
        /// </summary>
        /// <param name="MinSecend">时间间隔毫秒数</param>
        /// <param name="MethodName">方法名称</param>
        public void SetInterval(int MinSecend, string MethodName)
        {
            if (bolUserInteractive == false)
            {
                // 若不能和用户互换则退出处理
                return;
            }
            // 检查参数
            if (MethodName == null || MethodName.Trim().Length == 0)
            {
                return;
            }
            if (this.myEngine == null)
            {
                return;
            }

            if (myTimer != null)
            {
                //取消当前的延时调用功能
                myTimer.Stop();
            }
            strTimeoutMethod = null;

            if (myEngine.HasMethod(MethodName.Trim()) == false)
                return;
            strTimerIntervalMethod = MethodName.Trim();

            if (myIntervalTimer == null)
            {
                // 初始化定时调用的定时器控件
                myIntervalTimer = new System.Windows.Forms.Timer();
                myIntervalTimer.Tick += new EventHandler(myIntervalTimer_Tick);
            }

            myIntervalTimer.Interval = MinSecend;
        }
        /// <summary>
        /// 清除定时运行
        /// </summary>
        public void ClearInterval()
        {
            if (myIntervalTimer != null)
            {
                // 停止定时调用
                myIntervalTimer.Stop();
            }
            strTimerIntervalMethod = null;
        }
        /// <summary>
        /// 定时调用的定时器事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void myIntervalTimer_Tick(object sender, EventArgs e)
        {
            if (myIntervalTimer != null)
            {
                // 设置定时调用的脚本方法名称
                strTimerIntervalMethod = strTimerIntervalMethod.Trim();
            }
            if (strTimerIntervalMethod == null
                || strTimerIntervalMethod.Length == 0
                || myEngine == null
                || myEngine.HasMethod(strTimerIntervalMethod) == false)
            {
                if (myIntervalTimer != null)
                {
                    // 若没找到指定名称的脚本方法则停止定时调用
                    myIntervalTimer.Stop();
                }
                return;
            }
            // 安全的执行指定名称的脚本方法
            myEngine.ExecuteSub(strTimerIntervalMethod);
        }

        #endregion

        #region 控制窗体位置大小的代码群 **************************************************

        /// <summary>
        /// 窗体标题
        /// </summary>
        public string Title
        {
            get
            {
                System.Windows.Forms.Form frm = myParentWindow as System.Windows.Forms.Form;
                if (frm == null)
                {
                    return "";
                }
                else
                {
                    return frm.Text;
                }
            }
            set
            {
                System.Windows.Forms.Form frm = myParentWindow as System.Windows.Forms.Form;
                if (frm != null)
                {
                    frm.Text = value;
                }
            }
        }

        /// <summary>
        /// 窗体左端位置
        /// </summary>
        public int Left
        {
            get
            {
                System.Windows.Forms.Form frm = myParentWindow as System.Windows.Forms.Form;
                if (frm == null)
                    return 0;
                else
                    return frm.Left;
            }
            set
            {
                System.Windows.Forms.Form frm = myParentWindow as System.Windows.Forms.Form;
                if (frm != null)
                {
                    frm.Left = value;
                }
            }
        }

        /// <summary>
        /// 窗体顶端位置
        /// </summary>
        public int Top
        {
            get
            {
                System.Windows.Forms.Form frm = myParentWindow as System.Windows.Forms.Form;
                if (frm == null)
                    return 0;
                else
                    return frm.Top;
            }
            set
            {
                System.Windows.Forms.Form frm = myParentWindow as System.Windows.Forms.Form;
                if (frm != null)
                {
                    frm.Top = value;
                }
            }
        }

        /// <summary>
        /// 窗体宽度
        /// </summary>
        public int Width
        {
            get
            {
                System.Windows.Forms.Form frm = myParentWindow as System.Windows.Forms.Form;
                if (frm == null)
                    return 0;
                else
                    return frm.Width;
            }
            set
            {
                System.Windows.Forms.Form frm = myParentWindow as System.Windows.Forms.Form;
                if (frm != null)
                {
                    frm.Width = value;
                }
            }
        }

        /// <summary>
        /// 窗体高度
        /// </summary>
        public int Height
        {
            get
            {
                System.Windows.Forms.Form frm = myParentWindow as System.Windows.Forms.Form;
                if (frm == null)
                    return 0;
                else
                    return frm.Height;
            }
            set
            {
                System.Windows.Forms.Form frm = myParentWindow as System.Windows.Forms.Form;
                if (frm != null)
                {
                    frm.Height = value;
                }
            }
        }

        #endregion
        /// <summary>
        /// 将对象转化为用于显示的文本
        /// </summary>
        /// <param name="objData">要转换的对象</param>
        /// <returns>显示的文本</returns>
        private string GetDisplayText(object objData)
        {
            if (objData == null)
                return "[null]";
            else
                return Convert.ToString(objData);
        }

        /// <summary>
        /// 显示消息框
        /// </summary>
        /// <param name="objText">提示信息的文本</param>
        public void Alert(object objText)
        {
            if (bolUserInteractive == false)
                return;
            System.Windows.Forms.MessageBox.Show(
                myParentWindow,
                GetDisplayText(objText),
                SystemName,
                System.Windows.Forms.MessageBoxButtons.OK,
                System.Windows.Forms.MessageBoxIcon.Information);
        }
        /// <summary>
        /// 显示错误消息框
        /// </summary>
        /// <param name="objText">提示信息的文本</param>
        public void AlertError(object objText)
        {
            if (bolUserInteractive == false)
                return;
            System.Windows.Forms.MessageBox.Show(
                myParentWindow,
                GetDisplayText(objText),
                SystemName,
                System.Windows.Forms.MessageBoxButtons.OK,
                System.Windows.Forms.MessageBoxIcon.Exclamation);
        }

        /// <summary>
        /// 显示一个提示信息框,并返回用户的选择
        /// </summary>
        /// <param name="objText">提示的文本</param>
        /// <returns>用户是否确认的信息</returns>
        public bool ConFirm(object objText)
        {
            if (bolUserInteractive == false)
                return false;
            return (System.Windows.Forms.MessageBox.Show(
                myParentWindow,
                GetDisplayText(objText),
                SystemName,
                System.Windows.Forms.MessageBoxButtons.YesNo,
                System.Windows.Forms.MessageBoxIcon.Question)
                == System.Windows.Forms.DialogResult.Yes);
        }

        /// <summary>
        /// 显示一个信息输入框共用户输入
        /// </summary>
        /// <param name="objCaption">输入信息的提示</param>
        /// <param name="objDefault">默认值</param>
        /// <returns>用户输入的信息</returns>
        public string Prompt(object objCaption, object objDefault)
        {
            if (bolUserInteractive == false)
                return null;
            return dlgInputBox.InputBox(
                myParentWindow,
                GetDisplayText(objCaption),
                SystemName,
                GetDisplayText(objDefault));
        }

        /// <summary>
        /// 显示一个文本选择对话框
        /// </summary>
        /// <param name="objCaption">对话框标题</param>
        /// <param name="objFilter">文件过滤器</param>
        /// <returns>用户选择的文件名,若用户取消选择则返回空引用</returns>
        public string BrowseFile(object objCaption, object objFilter)
        {
            using (System.Windows.Forms.OpenFileDialog dlg
                       = new System.Windows.Forms.OpenFileDialog())
            {
                dlg.CheckFileExists = true;
                if (objCaption != null)
                {
                    dlg.Title = this.GetDisplayText(objCaption);
                }
                if (objFilter != null)
                    dlg.Filter = GetDisplayText(objFilter);
                if (dlg.ShowDialog(myParentWindow) == System.Windows.Forms.DialogResult.OK)
                    return dlg.FileName;
            }
            return null;
        }
        /// <summary>
        /// 显示一个文件夹选择对话框
        /// </summary>
        /// <param name="objCaption">对话框标题</param>
        /// <returns>用户选择了一个文件夹则返回该路径,否则返回空引用</returns>
        public string BrowseFolder(object objCaption)
        {
            using (System.Windows.Forms.FolderBrowserDialog dlg
                       = new System.Windows.Forms.FolderBrowserDialog())
            {
                if (objCaption != null)
                {
                    dlg.Description = this.GetDisplayText(objCaption);
                }
                dlg.RootFolder = System.Environment.SpecialFolder.MyComputer;
                if (dlg.ShowDialog(myParentWindow) == System.Windows.Forms.DialogResult.OK)
                    return dlg.SelectedPath;
                else
                    return null;
            }
        }

        /// <summary>
        /// 打印调试信息
        /// </summary>
        /// <param name="objText">调试信息的文本</param>
        public void DebugPrintLine(object objText)
        {
            System.Diagnostics.Debug.WriteLine(GetDisplayText(objText));
        }
        /// <summary>
        /// 当前处理暂停指定的毫秒数的时间
        /// </summary>
        /// <param name="MilliSecond">毫秒数</param>
        public void Sleep(int MilliSecond)
        {
            System.Threading.Thread.Sleep(MilliSecond);
        }

        public void Dispose()
        {
            if (myTimer != null)
            {
                myTimer.Dispose();
                myTimer = null;
            }
            if (myIntervalTimer != null)
            {
                myIntervalTimer.Dispose();
                myIntervalTimer = null;
            }
        }
    }//public class XVBAWindowObject : System.IDisposable}
}